﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ARR.Entities.Gateways;
using System.Data;
using DDF.Data.Providers;
using DDF.Data;
using System.Data.SqlClient;

namespace ARR.Model.Repositories
{
    public static class QueriesRepository
    {
        public static List<TopSalesCategoryRow> SelectTopSalesCategories()
        {
            List<TopSalesCategoryRow> topCategories = new List<TopSalesCategoryRow>();

            using (SqlDataProvider database = new SqlDataProvider(@"Data Source=.;Initial Catalog=ARR;Integrated Security=True", "{0}Cursor", "dbo"))
            {
                database.NewCommand("TopSalesCategories", System.Data.CommandType.StoredProcedure);
                
                IDataReader reader = database.ExecuteReader();

                Load(reader, topCategories);
            }

            return topCategories;
        }

        private static void Load(IDataReader reader, List<TopSalesCategoryRow> topCategories)
        {
            TopSalesCategoryRow row;

            while (reader.Read())
            {
                row = new TopSalesCategoryRow()
                {
                    Category = Util.CastDbValue<string>(reader["Name"]),
                    Total = Util.CastDbValue<decimal>(reader["Total"])
                };

                topCategories.Add(row);
            }

            reader.Close();
        }
    }
}
